unit uXilinxCSV ;

interface

procedure ConvertXilinxCSV( aPart, aFilenameIn, aFilenameOut : string ) ;

implementation

uses
    Classes,
    SysUtils,
    sdXmlDocuments ;

(*
SORT_ROW,SORT_COLUMN,PIN_NUMBER,XC3S50,XC350_TYPE,BANK
A,1,A1 ,TDI,JTAG,0
A,2,A2 ,PROG_B,CONFIG,VCCAUX
A,3,A3 ,IO_L01N_0/VRP_0,DCI,0
A,4,A4 ,VCCO_TOP,VCCO,"0,1"
A,5,A5 ,VCCAUX,VCCAUX,N/A
A,6,A6 ,IO_L30P_0,I/O,0
*)
(*
<?xml version="1.0"?>
<?chipdata-process type="ECPinPak"?>
<ECPinPak>
    <Instance ObjectID="MSP430F1132IRHBT"></Instance>
    <Pinmap shape="Rectangle" id="PM01">
        <Pin pol="N" name="RST_L_NMI" id="P5" dir="I" num="5" type="undefined"></Pin>
        <Pin pol="P" name="NC" id="P4" dir="U" num="4" type="undefined"></Pin>
*)

function Compare( List : TStringList ; Index1, Index2 : Integer ) : Integer ;
var
    a, b            : TStringlist ;
begin
    a := List.Objects[ Index1 ] as TStringlist ;
    b := List.Objects[ Index2 ] as TStringlist ;
    Result := CompareStr( a[ 3 ], b[ 3 ] ) ;
    if Result = 0 then
        Result := CompareStr( a[ 0 ], b[ 0 ] ) ;
end ;

procedure ConvertXilinxCSV( aPart, aFilenameIn, aFilenameOut : string ) ;
var
    iLines, iColumns : TStringlist ;
    aXMLDoc         : TsdXmlDocument ;
    aNode, aPin     : TXmlNode ;
    aBank           : string ;
    i               : integer ;

begin
    iLines := TStringlist.Create ;
    try
        iLines.LoadFromFile( aFilenameIn ) ;
        aXMLDoc := TsdXmlDocument.CreateName( 'ECPinPak' ) ;
        with aXMLDoc do try
            ExternalEncoding := se8bit ;
            XmlFormat := xfReadable ;
            CommentString := 'Created by ObjectEDA' ;
            aNode := Root.NodeNew( 'Instance' ) ;
            aNode.AttributeAdd( 'ObjectID', aPart ) ;
            iLines.Delete( 0 ) ;                  // remove header
            for i := 0 to iLines.Count - 1 do begin
                iColumns := TStringlist.Create ;
                iColumns.CommaText := iLines[ i ] ;
                iLines.Objects[ i ] := iColumns ;
            end ;
            iLines.CustomSort( Compare ) ;        // sort bank wise

            aBank := '' ;
            for i := 0 to iLines.Count - 1 do begin
                iColumns := iLines.Objects[ i ] as TStringlist ;
                if aBank <> iColumns[ 3 ] then begin
                    aNode := Root.NodeNew( 'PinMap' ) ;
                    aNode.AttributeAdd( 'shape', 'Rectangle' ) ;
                    aBank := iColumns[ 3 ] ;
                    aNode.AttributeAdd( 'id', aBank ) ;
                end ;
                aPin := aNode.NodeNew( 'Pin' ) ;
                aPin.AttributeAdd( 'pol', 'P' ) ;
                aPin.AttributeAdd( 'name', iColumns[ 1 ] ) ;
                aPin.AttributeAdd( 'id', iColumns[ 3 ] ) ;
                aPin.AttributeAdd( 'dir', 'I' ) ;
                aPin.AttributeAdd( 'num', iColumns[ 0 ] ) ;
                aPin.AttributeAdd( 'type', iColumns[ 2 ] ) ;
            end ;
        finally
            SaveToFile( aFilenameOut ) ;
            Free ;
        end ;
    finally
        for i := 0 to iLines.Count - 1 do
            iLines.Objects[ i ].Free ;
        iLines.Free ;
    end ;
end ;

end.

